﻿package display
{
	import controller.Controller;
	import data.ItemData;
	import data.PresetData;
	import events.DisplayEvent;
	import flash.display.ColorCorrection;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.system.System;
	import flash.text.TextField;
	import utils.Strings;
	
	import fl.controls.ColorPicker;
	import fl.events.ColorPickerEvent;
	import fl.motion.Color;
	import fl.controls.NumericStepper;
	import fl.controls.ComboBox; 
	import fl.data.DataProvider; 
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	public class DisplayTabBtn extends ScrollButton
	{
		private var itemName:String;
		private var type:String;
		private var itemCategory:String;
		private var category:String;
		private var itemData:ItemData;
		private var presetData:PresetData;
		
		private var cpOne:ColorPick;
		private var cpTwo:ColorPick;
		
		private var numericStepper:NumericStepper;
		
		private var copyIconBtn:CopyIcon
		private var copyIconBtnTwo:CopyIcon;
		
		private var colorOne:uint = 0x000000;
		private var colorTwo:uint = 0x000000;
		private var alphaValue:Number;
		private var fontType:String;
		
		private var control:Controller;
		
		private var overlay:Sprite;
		
		private var colortrans:ColorTransform;
		
		public function DisplayTabBtn(_itemName:String,_type:String,_category:String,_itemCategory:String,ctr:Controller,_presetItem:PresetData) 
		{
			super();
			
			
			itemName = _itemName;
			type = _type;
			category = _category;
			itemCategory = _itemCategory;
			presetData = _presetItem;
			
			control = ctr;
			control.addEventListener(DisplayEvent.ON_PRESET_CHANGED, onPresetChanged);
		}
		
		private function createElements():void
		{
			switch(type)
			{
				case 'gradient':
				
				var gArray:Array = new Array;
				gArray = String(presetData[itemName]).split(',');
				
				colorOne = gArray[0];
				colorTwo = gArray[1];
				
				createGradientButton();
				break;
				
				case 'colorpicker':
				colorOne = uint(presetData[itemName]);
				
				createColorPickerButton();
				break;
				
				case 'alpha':
				
				alphaValue = Number(presetData[itemName]);
				
				createAplhaButton();
				break;
				
				case 'font':
				
				createFontButton();
				break;
			}
		}
		
		private function createOverlay():Sprite
		{
			var s:Sprite = new Sprite;
			
			s.graphics.beginFill(0x000000, 0)
			s.graphics.drawRect(0, 0, 270, 30);
			s.graphics.endFill();
			s.buttonMode = true;
			s.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			s.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			return s;
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			dispatchEvent(new DisplayEvent(DisplayEvent.ON_HIGHLIGHT, { state:'over',item:itemCategory } ));
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			dispatchEvent(new DisplayEvent(DisplayEvent.ON_HIGHLIGHT, { state:'out',item:itemCategory } ));
		}
		
		private function createFontButton():void
		{
			var items:Array = new Array;
			var comboBox:ComboBox = new ComboBox;
			
			items = [ {label:'Verdana', data:'verdana' } ];
			
			comboBox.dropdownWidth = 210; 
			comboBox.width = 100;  
			comboBox.move(164, 3);
			comboBox.dataProvider = new DataProvider(items); 
			comboBox.addEventListener(Event.CHANGE, onFontChange); 
			
			addChild(comboBox);
			
			//dispatchEvent(new DisplayEvent(DisplayEvent.FONT_CHANGED, { font:'arial' } ));
		}
		
		private function createGradientButton():void
		{
			colortrans = new ColorTransform;
			colortrans.color = 0x666666;
			
			overlay = createOverlay();
			overlay.height = 60;
			
			bg.height = 60;
			
			copyIconBtn = new CopyIcon;
			MovieClip(copyIconBtn).transform.colorTransform = colortrans;
			copyIconBtn.x = 3;
			copyIconBtn.y = 6;
			copyIconBtn.buttonMode = true;
			copyIconBtn.addEventListener(MouseEvent.CLICK, onCopyToClipColorOne);
			
			cpOne = new ColorPick;
			cpOne.x = 240;
			cpOne.y = 3;
			cpOne.selectedColor = colorOne;
			cpOne.addEventListener(ColorPickerEvent.CHANGE, onColorOneChanged);
			
			copyIconBtnTwo = new CopyIcon;
			MovieClip(copyIconBtnTwo).transform.colorTransform = colortrans;
			copyIconBtnTwo.x = 3;
			copyIconBtnTwo.y = 36;
			copyIconBtnTwo.buttonMode = true;
			copyIconBtnTwo.addEventListener(MouseEvent.CLICK, onCopyToClipColorTwo)
			
			cpTwo = new ColorPick;
			cpTwo.x = 240;
			cpTwo.y = 30;
			cpTwo.selectedColor = colorTwo;
			cpTwo.addEventListener(ColorPickerEvent.CHANGE, onColorTwoChanged);
			
			addChild(overlay);
			addChild(copyIconBtn);
			addChild(copyIconBtnTwo);
			addChild(cpOne);
			addChild(cpTwo);
			
			dispatchEvent(new DisplayEvent(DisplayEvent.COLOR_CHANGED,{colorOne:colorOne,colorTwo:colorTwo,type:type,category:category,itemCategory:itemCategory}));
		}
		
		private function onCopyToClipColorOne(e:MouseEvent):void
		{
			System.setClipboard(Strings.getNumberAsHexString(colorOne,6));
		}
		
		private function onCopyToClipColorTwo(e:MouseEvent):void
		{
			System.setClipboard(Strings.getNumberAsHexString(colorTwo,6));
		}
		
		private function createColorPickerButton():void
		{
			colortrans = new ColorTransform;
			colortrans.color = 0x666666;
			
			overlay = createOverlay();
			
			copyIconBtn = new CopyIcon;
			MovieClip(copyIconBtn).transform.colorTransform = colortrans;
			copyIconBtn.x = 3;
			copyIconBtn.y = 6;
			copyIconBtn.buttonMode = true;
			copyIconBtn.addEventListener(MouseEvent.CLICK, onCopyToClipColorOne);
			
			cpOne = new ColorPick;
			cpOne.x = 240;
			cpOne.y = 3;
			cpOne.selectedColor = colorOne;
			cpOne.addEventListener(ColorPickerEvent.CHANGE, onColorOneChanged);
			
			addChild(overlay);
			addChild(copyIconBtn);
			addChild(cpOne);
			
			dispatchEvent(new DisplayEvent(DisplayEvent.COLOR_CHANGED,{colorOne:colorOne,colorTwo:colorTwo,type:type,category:category,itemCategory:itemCategory}));
		}
		
		private function createAplhaButton():void
		{
			numericStepper = new NumericStepper;
			numericStepper.stepSize = 1;
			numericStepper.minimum = 0;
			numericStepper.maximum = 100;
			numericStepper.width = 60;
			numericStepper.x = 204;
			numericStepper.y  = 3;
			numericStepper.value = alphaValue;
			numericStepper.addEventListener(Event.CHANGE,onAlphaChanged);
			
			addChild(numericStepper);
			
			dispatchEvent(new DisplayEvent(DisplayEvent.ALPHA_CHANGED, { alpha:alphaValue, itemCategory:itemCategory } ));
		}
		
		private function onPresetChanged(e:DisplayEvent):void
		{
			presetData = e.data.presetData;
			
			//clear elements
			
			while (this.numChildren != 2)
			{
				removeChildAt(0);
			}
			
			//create new elements
			
			createElements();
		}
		
		private function onFontChange(e:Event):void
		{
			dispatchEvent(new DisplayEvent(DisplayEvent.FONT_CHANGED,{ font:ComboBox(e.target).selectedItem.data}));
		}
		
		private function onAlphaChanged(e:Event):void
		{
			dispatchEvent(new DisplayEvent(DisplayEvent.ALPHA_CHANGED, { alpha:e.target.value,category:category,itemCategory:itemCategory} ));
		}
		
		private function onColorOneChanged(e:ColorPickerEvent):void
		{
			colorOne = e.color;
			
			dispatchEvent(new DisplayEvent(DisplayEvent.COLOR_CHANGED,{colorOne:colorOne,colorTwo:colorTwo,type:type,category:category,itemCategory:itemCategory}));
		}
		
		private function onColorTwoChanged(e:ColorPickerEvent):void
		{
			colorTwo = e.color;
			
			dispatchEvent(new DisplayEvent(DisplayEvent.COLOR_CHANGED,{colorOne:colorOne,colorTwo:colorTwo,type:type,category:category,itemCategory:itemCategory}));
		}
		
		private function onColorChanged(e:ColorPickerEvent):void
		{
			dispatchEvent(new DisplayEvent(DisplayEvent.COLOR_CHANGED,{colorOne:colorOne,colorTwo:colorTwo,type:type}));
		}
	}
}